import {
    addOrderInfo,
    addOrderInfoList,
    del,
    deleteByBatchId,
    findGoodList,
    GetdataDictionary,
    getGoodInfo,
    show
} from "@/api/b2c/order";

export default {
    name: "index",
    data() {
        return {
            batchAdd: false,
            tableData: [],
            value: [],
            idList: {},
            multipleSelection: [],
            formData: {
                orderType: ``,
                orderAction: ``,
                businessType: ``,
                paymentMethodId: ``,
                receiverName: ``,
                receiverAdress: ``,
                receiverPhone: ``,
                payName: ``
            },//添加的集合
            dialogFormVisible: false,
            //数据字典
            payTypeList: [],
            orderActionlist: [],
            orderTypeList: [],
            orderStatusList: [],
            businessTypeList: [],
            total: 0,
            searchParams: {currentPage: 1, pageSize: 5, brandId: "", typeId: "", colourId: ""},//商品查询条件
            goofInfo: {
                goodNumber: 1,
                goodMoney: 0,
            },
            good: {},
            //订单基本信息
            //添加订单信息汇总
            orderInfoSum: {
                orderType: ``,
                orderAction: ``,
                businessType: ``,
                paymentMethodId: ``,
                vipId: ``,
                deliveryStyle: ``,
                deliveryCos: ``,
                deliveryArea: ``,
                deliverySamecity: ``,
                deliveryInsurance: ``,
                receiverId: ``,
                returnId: ``,
                orderStatus: ``,
                //商品表需要信息
                // orderGoodS:[],
                modelId: ``,
                total: ``,
                goodsId: ``,
                brandId: ``,
                colourId: ``,
                orderProductMoney: ``,
                orderProductName: ``,
                typeId: ``,
                orderProductPrice: ``,
                orderProductNumber: ``,
                //收货人信息表
                receiverName: ``,
                postalCode: ``,
                receiverAdress: ``,
                receiverEmail: ``,
                receiverTelephone: ``,
                receiverPhone: ``,
                selfMentiontime: ``,
                receiverNotation: ``,
                payName: ``,
            },
            OrderGoodListInfo: [],
            goodInfo: {
                //订单信息
                orderType: ``,
                orderAction: ``,
                businessType: ``,
                paymentMethodId: ``,
                vipId: ``,
                deliveryStyle: ``,
                deliveryCos: ``,
                deliveryArea: ``,
                deliverySamecity: ``,
                deliveryInsurance: ``,
                receiverId: ``,
                returnId: ``,
                orderStatus: ``,
                //收货人信息
                receiverName: ``,
                postalCode: ``,
                receiverAdress: ``,
                receiverEmail: ``,
                receiverTelephone: ``,
                receiverPhone: ``,
                selfMentiontime: ``,
                receiverNotation: ``,
                payName: ``,
                //商品数组
                b2cOrderGoodsList: []
            },

            //表单验证
            rules: {
                receiverName: [
                    {required: true, message: '请输入收货人姓名', trigger: 'blur'},
                    {min: 2, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur'}
                ],
                paymentMethodId: [
                    {required: true, message: '请选择支付方式', trigger: 'change'}
                ],
                businessType: [
                    {required: true, message: '请选择支付方式', trigger: 'change'}
                ],
                orderType: [
                    {required: true, message: '请选择订单类型', trigger: 'change'}
                ],
                orderAction: [
                    {required: true, message: '请选择订单动作', trigger: 'change'}
                ],
                receiverAdress: [
                    {required: true, message: '请输入收货地址', trigger: 'blur'}
                ],
                receiverPhone: [
                    {required: true, message: '请输入收货电话', trigger: 'blur'}
                ],
                payName: [
                    {required: true, message: '请输入汇款人', trigger: 'blur'}
                ],

            }
        }
    },
    methods: {
        async shoppingList() {
            let response = await show();
            this.tableData = response
            // this.total = response.total
        },
        async back() {
            location.href = "/order/addOrder"
        },
        async querydic() {
            //1.订单动作 2 订单状态 3 业务类型 4 配送方式  5 支付方式  6订单分类订单类型 7 退货原因 ',
            let result = await GetdataDictionary();
            console.log(result)
            this.orderActionlist = result[1]
            this.orderStatusList = result[2]
            this.businessTypeList = result[3]
            this.payTypeList = result[5]
            this.orderTypeList = result[6]
            // console.log(this.orderTypeList)
        },
        //单个结算
        //1 点击结算 先添加订单信息
        //2 点击结算 先添加收货人信息
        //3 结算之后添加到销售订单详情
        async settlement(row) {
            this.good = await getGoodInfo(row.id)
            //商品信息
            this.orderInfoSum.modelId = this.good.modelId;
            this.orderInfoSum.brandId = this.good.brandId;
            this.orderInfoSum.colorId = this.good.colourId;
            this.orderInfoSum.goodsId = this.good.id;
            this.orderInfoSum.typeId = this.good.typeId;
            this.orderInfoSum.total = row.goodNumber
            this.orderInfoSum.orderProductNumber = row.goodNumber
            this.orderInfoSum.orderProductName = row.goodsName;
            this.orderInfoSum.orderProductPrice = row.goodPrice;
            this.orderInfoSum.orderProductMoney = row.goodNumber * row.goodPrice
            //收货人信息
            this.orderInfoSum.receiverName = this.formData.receiverName;
            this.orderInfoSum.receiverEmail = this.formData.receiverEmail
            this.orderInfoSum.receiverTelephone = this.formData.receiverTelephone
            this.orderInfoSum.receiverPhone = this.formData.receiverPhone
            this.orderInfoSum.payName = this.formData.payName
            this.orderInfoSum.postalCode = this.formData.postalCode
            this.orderInfoSum.receiverNotation = this.formData.receiverNotation
            this.orderInfoSum.vipId = this.formData.vipId
            this.orderInfoSum.selfMentiontime = this.formData.selfMentiontime
            //订单信息
            this.orderInfoSum.orderType = this.formData.orderType
            this.orderInfoSum.orderAction = this.formData.orderAction
            this.orderInfoSum.businessType = this.formData.businessType
            this.orderInfoSum.paymentMethodId = this.formData.paymentMethodId
            this.orderInfoSum.vipId = this.formData.vipId
            this.orderInfoSum.deliveryStyle = 1
            this.orderInfoSum.deliveryCos = 15
            this.orderInfoSum.deliveryArea = this.formData.receiverAdress
            this.orderInfoSum.returnId = ``
            // this.orderInfoSum.orderStatus = 11
        },
        //传递所有信息
        async transfer() {
            console.log(this.orderInfoSum);
            await addOrderInfo(this.orderInfoSum).then(response => {
                if (response > 0) {
                    this.open2()
                    this.shoppingList()
                }
            })
        },

        //选框  选中数据
        async handleSelectionChange(val) {
            this.multipleSelection = val;
            console.log(this.multipleSelection)
            this.idList = this.multipleSelection.map(item => item.id)
            this.OrderGoodListInfo = await findGoodList(this.idList)
            for (let i = 0; i < val.length; i++) {
                this.OrderGoodListInfo[i].orderProductMoney = val[i].goodNumber * val[i].goodPrice
                this.OrderGoodListInfo[i].orderProductNumber = val[i].goodNumber
                this.OrderGoodListInfo[i].colorId = this.OrderGoodListInfo[i].colourId
                this.OrderGoodListInfo[i].orderProductName = this.OrderGoodListInfo[i].goodsName
                this.OrderGoodListInfo[i].orderProductPrice = this.OrderGoodListInfo[i].mallPrice
                this.OrderGoodListInfo[i].total = val[i].goodNumber
                this.OrderGoodListInfo[i].goodsId = this.OrderGoodListInfo[i].id
            }
            console.log(this.OrderGoodListInfo)
        },
        //批量结算
        async batchAddOrder() {
            this.goodInfo.deliveryStyle = 1
            this.goodInfo.deliveryCos = 15
            this.goodInfo.deliveryArea = this.goodInfo.receiverAdress
            this.goodInfo.returnId = ``
            // this.goodInfo.orderStatus = 11
            //商品信息
            this.goodInfo.b2cOrderGoodsList = this.OrderGoodListInfo
            console.log(this.goodInfo)
            await addOrderInfoList(this.goodInfo).then(response => {
                if (response > 0) {
                    this.open2()
                    this.shoppingList()
                }
            })
        },
        //批量删除购物车商品
        async deleteGoods() {
            this.idList = this.multipleSelection.map(item => item.id)
            await deleteByBatchId(this.idList)
            await this.shoppingList()
        },
        //单个删除购物车中商品
        async deleteGoodsById(row) {
            await del(row.id)
            await this.shoppingList()
        },
        open2() {
            this.$message({
                message: '结算成功',
                type: 'success'
            });
        },
    },

    created() {
        this.shoppingList()
        this.querydic()
    }

}